package com.tanhua.controller;

import com.tanhua.Intercepter.UserHolder;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.service.UserInfoService;
import com.tanhua.service.UserService;
import com.tanhua.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 根据id查询用户信息
     *
     * @param id
     * @return
     */
    @GetMapping()
    public ResponseEntity findById(Long id) {
        //判断是否携带参数id，没有就使用token的id
        if (id == null) {

            id = Long.valueOf(UserHolder.getId());
        }
        UserInfoVo userInfo = userInfoService.findById(id);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 修改用户资料
     *
     * @param userInfo
     * @return
     */
    @PutMapping
    public ResponseEntity UpdataUser(@RequestBody UserInfo userInfo) {

        userInfo.setId(UserHolder.getId());
        userInfoService.updateUserInfo(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 更换头像
     * @param  headPhoto
     * @return
     */
    @PostMapping("/header")
    public ResponseEntity updateHead(@RequestBody MultipartFile headPhoto) throws IOException {
        Long id = UserHolder.getId();
        userInfoService.updateHead(headPhoto,id);
        return ResponseEntity.ok(null);
    }
}
